import { ifs, ts, Tsrv } from '@/services';
import { N_cPaintCodeGuide } from './type';

class T_cmd {
  // 主组件箱
  private compBox: ifs.I_compBox;

  // 主组件ID
  private compID: string;

  // 主组件实例
  private self: N_cPaintCodeGuide.cPaintCodeGuide;
  // 主组refs

  // 主组件created里初始化
  public init(
    self: N_cPaintCodeGuide.cPaintCodeGuide,
    compBoxInfo: ifs.I_compBoxInfo
  ) {
    this.self = self;
    this.compBox = compBoxInfo.TcompBox;
    this.compID = compBoxInfo.compID;
  }

  public getTcmd(): T_cmd {
    return this;
  }

  // 操作数据
  public operateData(
    tableName: string,
    type: 'insert' | 'edit',
    row: ifs.I_fv[],
    filter = {}
  ): Promise<boolean> {
    const ctd = new ts.T_ctdArg();
    ctd
      .setTableName(tableName)
      .setDbType(Tsrv.globalVar.dbType.MYSQL)
      .setRule({} as ifs.I_rule);
    if (type === 'insert') {
      const baseData = new ts.T_baseData();
      baseData
        .setDataRule({} as ifs.I_rule)
        .setFileInfo({})
        .setRowData(...row);
      ctd.setInsRecDataMany({}, baseData);
    } else {
      const ctdUpdData = new ts.T_ctdUpdData();
      ctdUpdData.setFilter(filter);
      ctdUpdData
        .setDataRule({} as ifs.I_rule)
        .setFileInfo({})
        .setRowData(...row);
      ctd.setUpdRecDataMany(ctdUpdData);
    }
    return new Promise((resolve, reject) => {
      Tsrv.batRows(ctd).then(([b, res, msg]) => {
        if (!b) {
          this.self.$msg(msg);
          reject(false);
        } else {
          resolve(true);
        }
      });
    });
  }
}
interface I_cmd extends T_cmd {}
export { I_cmd, T_cmd };
